﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ViewListCourse : System.Web.UI.Page
{
    DataSourceSelectArguments dc;
    protected void Page_Load(object sender, EventArgs e)
    {
        dc = new DataSourceSelectArguments();
    }
    protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if(e.CommandName == "Detail")
        {
            Session["CourseID"] = ((HiddenField)e.Item.FindControl("hiddenID")).Value;
            Response.Redirect("CourseDetails.aspx");
        }

        if (e.CommandName == "Select")
        {
            String courseID = ((HiddenField)e.Item.FindControl("hiddenID")).Value;
            String userid = (String)Session["UserID"];
            String date = DateTime.Now.ToString();
            String query = "INSERT INTO register_list(user_id, course_id) VALUES ('" + userid + "','" + courseID + "')";
            SqlDataSource2.InsertCommand = query;
            SqlDataSource2.Insert();

            Button btnSelect = (Button)e.Item.FindControl("btnSelect");
            Button btnUnSelect = (Button)e.Item.FindControl("btnUnSelect");
            btnSelect.Visible = false;
            btnUnSelect.Visible = true;
        }

        if (e.CommandName == "UnSelect")
        {
            String courseID = ((HiddenField)e.Item.FindControl("hiddenID")).Value;
            String userid = (String)Session["UserID"];
            String date = DateTime.Now.ToString();
            String query = "DELETE FROM register_list WHERE user_id='" + userid + "' AND course_id='" + courseID + "'";
            SqlDataSource2.DeleteCommand = query;
            SqlDataSource2.Delete();

            Button btnSelect = (Button)e.Item.FindControl("btnSelect");
            Button btnUnSelect = (Button)e.Item.FindControl("btnUnSelect");
            btnSelect.Visible = true;
            btnUnSelect.Visible = false;
        }
    }
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        String courseID = ((HiddenField)e.Item.FindControl("hiddenID")).Value;
        if(Session["UserID"] != null)
        {
            String userid = (String)Session["UserID"];
            String query = "SELECT * FROM register_list WHERE user_id = '" + userid + "' AND course_id='" + courseID + "'";
            SqlDataSource2.SelectCommand = query;
            DataView dv = (DataView)SqlDataSource2.Select(dc);

            Button btnSelect = (Button)e.Item.FindControl("btnSelect");
            Button btnUnSelect = (Button)e.Item.FindControl("btnUnSelect");
            if(dv.Count != 0)
            {
                btnSelect.Visible = false;
                btnUnSelect.Visible = true;
            }
            else
            {
                btnSelect.Visible = true;
                btnUnSelect.Visible = false;
            }
        }
    }

}